<?php
// +-------------------------------------------------------------------
// | 
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Admin\Controller;

use Kcdns\Admin\Controller\CommonController;
use Think\Model;

class DataController extends CommonController
{

    const ORDER_STATE = [
        '15' => '待付款',
        '16' => '已付款',
        '17' => '已完成',
        '18' => '已结算',
    ];
    
    public function packets()
    {
    	
    	$list = O('data')->getPacketsList(I('get.start_time'), I('get.end_time'));
    	if (I('get.is_load') == 1) {
    		O('data')->explodePackets($list);
    		exit();
    	}
    	$this->assign([
    			'list' => $list,
    			
    			]);
    	$this->display();
    }
    
    public function count()
    {
    	$list = O('data')->count(I('get.start_time'), I('get.end_time'));
    	$this->assign('company', $list);
    	$this->display();
    }

    public function getalldata(){
        $today = date("Y-m-d");
        $yesterday = date("Y-m-d", strtotime(date("Y-m-d"))-86400);
        $thismonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
        $thismonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
        $lastmonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
        $lastmonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
        $today_input_count =  M('zs_order_pay')->where(['order_time'=>['egt',$today],'validCode'=>['in','15,16,17,18']])->count();
        $today_order_count = M('zs_order_pay')->where(['order_time'=>['egt',$today],'validCode'=>['in','16,17,18']])->count();
        $today_money = M('zs_order_pay')->where(['order_time'=>['egt',$today],'validCode'=>['in','16,17,18']])->sum('price');
        $today_yugu = M('zs_order_pay')->where(['order_time'=>['egt',$today],'validCode'=>['in','16,17,18']])->sum('estimateCosPrice');
        $yestoday_order_count = M('zs_order_pay')->where(['order_time'=>['between',[$yesterday,$today]],'validCode'=>['in','15,16,17,18']])->count();
        $yestoday_yugu = M('zs_order_pay')->where(['order_time'=>['between',[$yesterday,$today]],'validCode'=>['in','16,17,18']])->sum('pay_service_money');

        $thismon_input = M('zs_order_pay')->where(['order_time'=>['between',[$thismonth_start,$thismonth_end]],'validCode'=>['in','15,16,17,18']])->count();
        $thismon_order = M('zs_order_pay')->where(['order_time'=>['between',[$thismonth_start,$thismonth_end]],'validCode'=>['in','16,17,18']])->count();
        $ss = M('zs_order_pay')->getlastsql();
        $thismon_yugu = M('zs_order_pay')->where(['order_time'=>['between',[$thismonth_start,$thismonth_end]],'validCode'=>['in','16,17,18']])->sum('estimateCosPrice');

        $lastmon_input = M('zs_order_pay')->where(['order_time'=>['between',[$lastmonth_start,$lastmonth_end]],'validCode'=>['in','15,16,17,18']])->count();
        $lastmon_order = M('zs_order_pay')->where(['order_time'=>['between',[$lastmonth_start,$lastmonth_end]],'validCode'=>['in','16,17,18']])->count();
        $lastmon_yugu = M('zs_order_pay')->where(['order_time'=>['between',[$lastmonth_start,$lastmonth_end]],'validCode'=>['in','16,17,18']])->sum('estimateCosPrice');


        $data = [
            'today_input_count'=>$today_input_count,
            'today_order_count'=>$today_order_count,
            'today_money'=>$today_money,
            'today_yugu'=>$today_yugu,
            'yestoday_order_count'=>$yestoday_order_count,
            'yestoday_yugu'=>$yestoday_yugu,
            'thismon_input'=>$thismon_input,
            'thismon_order'=>$thismon_order,
            'thismon_yugu'=>$thismon_yugu,
            'lastmon_input'=>$lastmon_input,
            'lastmon_order'=>$lastmon_order,
            'lastmon_yugu'=>$lastmon_yugu,
        ];

        return $data;
    }

    public function kongzhitai(){

        import('ORG.Util.Page');// 导入分页类

        $today = date("Y-m-d");

        $data = $this->getalldata();


            $time1 = I('start_time');
            $time2 = I('end_time');

        if ($time1 && $time2) {
            $time1 = $time1 . ' 0:0:0';
            $time2 = $time2 . ' 23:59:59';
            $where2 = ['order_time'=>['between', [$time1, $time2]],'is_pay'=>1];
        }else{
            $where2 = ['order_time'=>['egt',$today],'is_pay'=>1];
        }

        $memberlist = M('member')->select();
        $paihangbang = [];
        if($memberlist){
            foreach ($memberlist as $k =>$v){

                $where21 = array_merge($where2,['mid'=>$v['uid']]);

                $yugu = M('zs_order_pay')->where($where21)->sum('pay_service_money');
                $paihangbang[$k]['username']= $v['nickname'];
                $paihangbang[$k]['money']= $yugu?:0;
                $paihangbang[$k]['jointime']= date('Y-m-d',$v['reg_time']);
            }
        }
        $paihangbang = $this->arraySort($paihangbang,'money',$sort='desc');

        $gongsibang = [];
        $actcom = M('zs_activity')->select();
        if($actcom){
            foreach ($actcom as $kk =>$vv){
                $where22 = array_merge($where2,['taobao_act_id'=>$vv['taobao_act_id']]);
                $ordermoney = M('zs_order_pay')->where($where22)->sum('price');
                $yugu_money = M('zs_order_pay')->where($where22)->sum('yugu_money');
                $todayorder_count = M('zs_order_pay')->where($where22)->count();

                $days = $this->calcTime($today,$vv['end_time']);

                $gongsibang[$kk]['activity_title'] = $vv['activity_title'];
                $gongsibang[$kk]['taobao_act_id'] = $vv['taobao_act_id'];
                $gongsibang[$kk]['start_time'] = $vv['start_time'];
                $gongsibang[$kk]['end_time'] = $vv['end_time'];
                $gongsibang[$kk]['days'] = $days;
                $gongsibang[$kk]['todayordercount'] = $todayorder_count;
                $gongsibang[$kk]['ordermoney'] = $ordermoney;
                $gongsibang[$kk]['yugu_money'] = $yugu_money;
                if($todayorder_count<=0){
                    unset($gongsibang[$kk]);
                }
            }
        }

        $this->assign('alldata',$data);
        $this->assign('gongsibang',$gongsibang);
        $this->assign('paihangbang',$paihangbang);
        $this->display();
    }

//orderlist
    public function orderlist()
    {

        import('ORG.Util.Page');// 导入分页类

        $data = $this->getalldata();
        $model = new Model();
//        $zs_order_pay = M('zs_order_pay');
        $zs_order_pay = $model->table('__ZS_ORDER_PAY__ as u');

        $today = date("Y-m-d");
        $ordersn = I('ordersn');
        $time1 = I('start_time');
        $time2 = I('end_time');
        $validCode = I('validCode');

        if ($time1 && $time2) {
            $time1 = $time1 . ' 0:0:0';
            $time2 = $time2 . ' 23:59:59';
            $where2 = ['u.order_time'=>['between', [$time1, $time2]],'is_pay'=>1];
        }else{
            $where2 = ['u.order_time'=>['egt',$today],'is_pay'=>1];
        }

        if($ordersn){
            $where2 = array_merge($where2,['order_no'=>$ordersn]);
        }

        if($validCode == 15 || $validCode == 16 || $validCode == 17 || $validCode == 18){
            $where2['u.validCode'] = $validCode;
        }

        if($validCode == 30){
            $where2['u.validCode'] = ['LT', 15];
        }

        $p = $this->getpage( $zs_order_pay ,$where2,10);



        $ss =  $zs_order_pay ->field('u.*,g.*')
//            ->alias('u')
            ->join('__ZS_GOODS__ g on g.skuId = u.skuId')
//            ->join('__SELLER__ s on s.shopId = g.shopId')
            ->where($where2)
            ->order('u.id desc')
            ->select();
        foreach($ss as &$item){
            $item['order_state_label'] = self::ORDER_STATE[$item['validCode']]?self::ORDER_STATE[$item['validCode']]:'无效';
        }
        $aa = $zs_order_pay->getLastSql();

        $this->assign('alldata',$data);
        $this->assign('list', $ss);
        $this->assign('page',$p->show());
        $this->assign('validCode', I('get.validCode')?I('get.validCode'):1);
        $this->display();
    }


    //分页
    public function getpage(&$m,$where,$pagesize=10){
        $m1=clone $m;//浅复制一个模型
        $count = $m->where($where)->count();//连惯操作后会对join等操作进行重置
        $m=$m1;//为保持在为定的连惯操作，浅复制一个模型
        $p=new \Think\Page($count,$pagesize);
        $p->lastSuffix=false;

//	    $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $p->setConfig('prev','上一页');
        $p->setConfig('next','下一页');
//	    $p->setConfig('last','末页');
//	    $p->setConfig('first','首页');
//	    $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

//	    $p->parameter=I('get.');

        $m->limit($p->firstRow,$p->listRows);

        return $p;
    }



    function arraySort($array,$keys,$sort='asc') {
        $newArr = $valArr = array();
        foreach ($array as $key=>$value) {
            $valArr[$key] = $value[$keys];
        }
        ($sort == 'asc') ?  asort($valArr) : arsort($valArr);
        reset($valArr);
        foreach($valArr as $key=>$value) {
            $newArr[$key] = $array[$key];
        }
        return $newArr;
    }

    public function ranking(){

        $today = date("Y-m-d");

        $today_yugu = M('zs_order_pay')->where(['order_time'=>['egt',$today],'is_pay'=>1])->sum('pay_service_money');

        $data = [
            'today_yugu'=>$today_yugu,
        ];

        $where2 = ['order_time'=>['egt',$today],'is_pay'=>1];

        $memberlist = M('member')->select();
        $paihangbang = [];
        if($memberlist){
            foreach ($memberlist as $k =>$v){

                $where2 = array_merge($where2,['mid'=>$v['uid']]);

                $yugu = M('zs_order_pay')->where($where2)->sum('pay_service_money');
                $paihangbang[$k]['username']= $v['nickname'];
                $paihangbang[$k]['money']= $yugu?:0;
            }
        }
        $paihangbang = $this->arraySort($paihangbang,'money',$sort='desc');

        $this->assign('alldata',$data);
        $this->assign('paihangbang',$paihangbang);
        $this->display();
    }

    function calcTime($fromTime, $toTime){

        //转时间戳
        $fromTime = strtotime($fromTime);
        $toTime = strtotime($toTime);
        //计算时间差
        $newTime = $toTime - $fromTime;
        return round($newTime / 86400) . '天';

    }

    public function sellerlist()
    {

        import('ORG.Util.Page');// 导入分页类

        $data = $this->getalldata();
        $model = new Model();
//        $zs_order_pay = M('zs_order_pay');
        $zs_order_pay = $model->table('__ZS_ORDER_PAY__ as u');

        $today = date("Y-m-d");
        $ordersn = I('ordersn');
        $time1 = I('start_time');
        $time2 = I('end_time');
        $validCode = I('validCode');

        if ($time1 && $time2) {
            $time1 = $time1 . ' 0:0:0';
            $time2 = $time2 . ' 23:59:59';
            $where2 = ['u.order_time'=>['between', [$time1, $time2]],'is_pay'=>1];
        }else{
            $where2 = ['u.order_time'=>['egt',$today],'is_pay'=>1];
        }

        if($ordersn){
            $where2 = array_merge($where2,['order_no'=>$ordersn]);
        }

        if($validCode == 15 || $validCode == 16 || $validCode == 17 || $validCode == 18){
            $where2['u.validCode'] = $validCode;
        }

        $where2['mid'] = session('user_auth.uid');

        if($validCode == 30){
            $where2['u.validCode'] = ['LT', 15];
        }

        $p = $this->getpage( $zs_order_pay ,$where2,10);



        $ss =  $zs_order_pay ->field('u.*,g.*')
//            ->alias('u')
            ->join('__ZS_GOODS__ g on g.skuId = u.skuId')
//            ->join('__SELLER__ s on s.shopId = g.shopId')
            ->where($where2)
            ->order('u.id desc')
            ->select();
        foreach($ss as &$item){
            $item['order_state_label'] = self::ORDER_STATE[$item['validCode']]?self::ORDER_STATE[$item['validCode']]:'无效';
        }

        $aa = $zs_order_pay->getLastSql();


        $gongsibang = [];
        $actcom = M('zs_seller')->select();
        if($actcom){
            foreach ($actcom as $kk =>$vv){
                $activity = M('zs_activity')->where(['taobao_act_id'=>$vv['taobao_act_id']])->find();
                $where22 = array_merge($where2,['taobao_act_id'=>$vv['taobao_act_id']]);
                $ordermoney = M('zs_order_pay')->where($where22)->sum('price');
                $yugu_money = M('zs_order_pay')->where($where22)->sum('yugu_money');
                $todayorder_count = M('zs_order_pay')->where($where22)->count();

                $days = $this->calcTime($today,$activity['end_time']);

                $tmp['seller_title'] = $vv['seller_title'];
                $tmp['taobao_act_id'] = $vv['taobao_act_id'];
                $tmp['start_time'] = $activity['start_time'];
                $tmp['end_time'] = $activity['end_time'];
                $tmp['days'] = $days;
                $tmp['todayordercount'] = $todayorder_count;
                $tmp['ordermoney'] = $ordermoney;
                $tmp['yugu_money'] = $yugu_money;
                $gongsibang[] = $tmp;
                /*if($todayorder_count<=0){

                }*/
            }
        }
//        var_dump($gongsibang);exit();

        $this->assign('alldata',$data);
        $this->assign('list', $ss);
        $this->assign('page',$p->show());
        $this->assign('validCode', I('get.validCode')?I('get.validCode'):1);
        $this->assign('gongsibang',$gongsibang);
        $this->display();
    }
}